package snafu.gui;

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;
import javax.swing.JTextArea;

import snafu.framework.worlds.Position;
import snafu.framework.worlds.Tile;
import snafu.framework.worlds.WorldMap;
import snafu.framework.worlds.WorldMapPanel;

public class DetailsPanel extends JPanel implements MouseListener, Observer {
	private WorldMap map;
	private WorldMapPanel mapPanel;
	private JTextArea detailsTextArea;
	
	public DetailsPanel(WorldMap map, WorldMapPanel mapPanel) {
		this.map = map;
		this.mapPanel = mapPanel;
		
		mapPanel.addMouseListener(this);
		
		setLayout(new BorderLayout());
		
		detailsTextArea = new JTextArea();
		detailsTextArea.setLineWrap(true);
		detailsTextArea.setEditable(false);
		add(detailsTextArea, BorderLayout.CENTER);
		
		
		
	}
	
	private void updateDetails() {
		Position position = mapPanel.getSelectedPosition();
		Tile tile = map.get(position);
		if (position == null) {
			detailsTextArea.setText("");
		} else {
			String details = position + "\n";
			details += tile.getWorldObjects();
			detailsTextArea.setText(details);
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
	}

	@Override
	public void mousePressed(MouseEvent e) {
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// set selected tile
		double tileScale = mapPanel.getTileScale();
		Tile tile = map.getTile(e.getX() / tileScale, e.getY() / tileScale);
		mapPanel.setSelectedPosition(tile.getPosition());

		// repaint map
		mapPanel.repaint();
		
		// show details
		updateDetails();
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}
	
	@Override
	public void update(Observable o, Object arg) {
		updateDetails();
	}
}
